/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.client.ZCalDataSource;
/*     */ import com.zimbra.client.ZDataSource;
/*     */ import com.zimbra.client.ZFolder;
/*     */ import com.zimbra.client.ZFolder.View;
/*     */ import com.zimbra.client.ZGrant.GranteeType;
/*     */ import com.zimbra.client.ZMailbox;
/*     */ import com.zimbra.client.ZMessage;
/*     */ import com.zimbra.client.ZRssDataSource;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.AccountConstants;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.Element.XMLElement;
/*     */ import com.zimbra.common.soap.MailConstants;
/*     */ import com.zimbra.common.util.HttpUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Cos;
/*     */ import com.zimbra.cs.account.DataSource;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.soap.admin.type.DataSourceType;
/*     */ import com.zimbra.soap.type.DataSource.ConnectionType;
/*     */ import java.util.ArrayList;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import junit.framework.TestCase;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestDataSource
/*     */   extends TestCase
/*     */ {
/*     */   private static final String USER_NAME = "user1";
/*     */   private static final String DS_NAME = "TestDataSource";
/*     */   private static final String TEST_USER_NAME = "testdatasource";
/*  54 */   private static final String NAME_PREFIX = TestDataSource.class.getSimpleName();
/*     */   
/*     */   private String mOriginalAccountPollingInterval;
/*     */   
/*     */   private String mOriginalAccountPop3PollingInterval;
/*     */   private String mOriginalAccountImapPollingInterval;
/*     */   private String mOriginalCosPollingInterval;
/*     */   private String mOriginalCosPop3PollingInterval;
/*     */   private String mOriginalCosImapPollingInterval;
/*     */   
/*     */   public void setUp()
/*     */     throws Exception
/*     */   {
/*  67 */     cleanUp();
/*     */     
/*     */ 
/*  70 */     Account account = TestUtil.getAccount("user1");
/*  71 */     Cos cos = account.getCOS();
/*  72 */     this.mOriginalAccountPollingInterval = account.getAttr("zimbraDataSourcePollingInterval", false);
/*  73 */     if (this.mOriginalAccountPollingInterval == null) {
/*  74 */       this.mOriginalAccountPollingInterval = "";
/*     */     }
/*  76 */     this.mOriginalAccountPop3PollingInterval = account.getAttr("zimbraDataSourcePop3PollingInterval", false);
/*  77 */     if (this.mOriginalAccountPop3PollingInterval == null) {
/*  78 */       this.mOriginalAccountPop3PollingInterval = "";
/*     */     }
/*  80 */     this.mOriginalAccountImapPollingInterval = account.getAttr("zimbraDataSourceImapPollingInterval", false);
/*  81 */     if (this.mOriginalAccountImapPollingInterval == null) {
/*  82 */       this.mOriginalAccountImapPollingInterval = "";
/*     */     }
/*     */     
/*  85 */     this.mOriginalCosPollingInterval = cos.getAttr("zimbraDataSourcePollingInterval", "");
/*  86 */     this.mOriginalCosPop3PollingInterval = cos.getAttr("zimbraDataSourcePop3PollingInterval", "");
/*  87 */     this.mOriginalCosImapPollingInterval = cos.getAttr("zimbraDataSourceImapPollingInterval", "");
/*     */   }
/*     */   
/*     */   public void testPollingInterval()
/*     */     throws Exception
/*     */   {
/*  93 */     Provisioning prov = Provisioning.getInstance();
/*  94 */     Account account = TestUtil.getAccount("user1");
/*  95 */     Map<String, Object> attrs = new HashMap();
/*  96 */     attrs.put("zimbraDataSourceEnabled", "FALSE");
/*  97 */     attrs.put("zimbraDataSourceHost", "testhost");
/*  98 */     attrs.put("zimbraDataSourcePort", "0");
/*  99 */     attrs.put("zimbraDataSourceUsername", "testuser");
/* 100 */     attrs.put("zimbraDataSourcePassword", "testpass");
/* 101 */     attrs.put("zimbraDataSourceFolderId", "1");
/* 102 */     attrs.put("zimbraDataSourceConnectionType", DataSource.ConnectionType.cleartext.toString());
/* 103 */     DataSource ds = prov.createDataSource(account, DataSourceType.pop3, NAME_PREFIX + " testPollingInterval", attrs);
/*     */     
/*     */ 
/* 106 */     assertNotNull("Min not defined", account.getAttr("zimbraDataSourceMinPollingInterval"));
/* 107 */     long min = account.getTimeInterval("zimbraDataSourceMinPollingInterval", 0L) / 1000L;
/* 108 */     attrs.clear();
/* 109 */     attrs.put("zimbraDataSourcePollingInterval", Long.toString(min));
/* 110 */     prov.modifyDataSource(account, ds.getId(), attrs);
/*     */     
/*     */ 
/* 113 */     attrs.clear();
/* 114 */     attrs.put("zimbraDataSourcePollingInterval", Long.toString(min - 1L));
/*     */     try {
/* 116 */       prov.modifyDataSource(account, ds.getId(), attrs);
/* 117 */       fail("modifyDataSource() was not supposed to succeed");
/*     */     } catch (ServiceException e) {
/* 119 */       assertTrue("Unexpected message: " + e.getMessage(), e.getMessage().contains("shorter than the allowed minimum"));
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testErrorStatus()
/*     */     throws Exception
/*     */   {
/* 130 */     Account account = TestUtil.createAccount("testdatasource");
/*     */     
/*     */ 
/* 133 */     Provisioning prov = Provisioning.getInstance();
/* 134 */     Map<String, Object> attrs = new HashMap();
/* 135 */     attrs.put("zimbraDataSourceEnabled", "TRUE");
/* 136 */     attrs.put("zimbraDataSourceHost", "localhost");
/* 137 */     attrs.put("zimbraDataSourcePort", TestUtil.getServerAttr("zimbraPop3BindPort"));
/* 138 */     attrs.put("zimbraDataSourceUsername", "user2");
/* 139 */     attrs.put("zimbraDataSourcePassword", "test123");
/* 140 */     attrs.put("zimbraDataSourceFolderId", Integer.toString(2));
/* 141 */     attrs.put("zimbraDataSourceConnectionType", DataSource.ConnectionType.cleartext.toString());
/* 142 */     attrs.put("zimbraDataSourceLeaveOnServer", "TRUE");
/* 143 */     DataSource ds = prov.createDataSource(account, DataSourceType.pop3, "TestDataSource", attrs);
/*     */     
/*     */ 
/* 146 */     ZMailbox mbox = TestUtil.getZMailbox("testdatasource");
/* 147 */     confirmErrorStatus(mbox, null);
/*     */     
/*     */ 
/* 150 */     ZDataSource zds = TestUtil.getDataSource(mbox, "TestDataSource");
/* 151 */     TestUtil.importDataSource(zds, mbox, null, true);
/* 152 */     confirmErrorStatus(mbox, null);
/*     */     
/*     */ 
/* 155 */     attrs.clear();
/* 156 */     attrs.put("zimbraDataSourcePassword", "bogus");
/* 157 */     prov.modifyDataSource(account, ds.getId(), attrs);
/* 158 */     zds = TestUtil.getDataSource(mbox, "TestDataSource");
/* 159 */     long startTimestamp = System.currentTimeMillis() - 1000L;
/* 160 */     TestUtil.importDataSource(zds, mbox, null, false);
/* 161 */     confirmErrorStatus(mbox, Long.valueOf(startTimestamp));
/*     */     
/*     */ 
/* 164 */     attrs.put("zimbraDataSourcePassword", "test123");
/* 165 */     prov.modifyDataSource(account, ds.getId(), attrs);
/* 166 */     confirmErrorStatus(mbox, null);
/*     */     
/*     */ 
/* 169 */     zds = TestUtil.getDataSource(mbox, "TestDataSource");
/* 170 */     startTimestamp = System.currentTimeMillis();
/* 171 */     TestUtil.importDataSource(zds, mbox, null, true);
/* 172 */     confirmErrorStatus(mbox, null);
/*     */   }
/*     */   
/*     */   private void confirmErrorStatus(ZMailbox mbox, Long laterThanTimestamp)
/*     */     throws Exception
/*     */   {
/* 178 */     Element request = new Element.XMLElement(AccountConstants.GET_INFO_REQUEST);
/* 179 */     Element response = mbox.invoke(request);
/* 180 */     Element eDS = response.getElement("dataSources");
/* 181 */     Element ePop3 = null;
/* 182 */     for (Element e : eDS.listElements("pop3")) {
/* 183 */       if (e.getAttribute("name").equals("TestDataSource")) {
/* 184 */         ePop3 = e;
/*     */       }
/*     */     }
/* 187 */     assertNotNull("Could not find data source in response: " + response.prettyPrint(), ePop3);
/* 188 */     confirmErrorStatus(ePop3, laterThanTimestamp);
/*     */     
/*     */ 
/* 191 */     ePop3 = null;
/* 192 */     request = new Element.XMLElement(MailConstants.GET_DATA_SOURCES_REQUEST);
/* 193 */     response = mbox.invoke(request);
/* 194 */     for (Element e : response.listElements("pop3")) {
/* 195 */       if (e.getAttribute("name").equals("TestDataSource")) {
/* 196 */         ePop3 = e;
/*     */       }
/*     */     }
/* 199 */     assertNotNull("Could not find data source in response: " + response.prettyPrint(), ePop3);
/* 200 */     confirmErrorStatus(ePop3, laterThanTimestamp);
/*     */   }
/*     */   
/*     */   private void confirmErrorStatus(Element ePop3, Long timestampBeforeSync) throws Exception
/*     */   {
/* 205 */     if (timestampBeforeSync != null) {
/* 206 */       assertTrue(ePop3.getElement("lastError").getText().length() > 0);
/* 207 */       long failingSince = ePop3.getAttributeLong("failingSince") * 1000L;
/* 208 */       long now = System.currentTimeMillis();
/*     */       
/* 210 */       assertTrue(failingSince + " is earlier than " + timestampBeforeSync, failingSince >= timestampBeforeSync.longValue());
/* 211 */       assertTrue(failingSince + " is later than " + now, failingSince < now);
/*     */     } else {
/* 213 */       assertNull("Last error was not reset", ePop3.getOptionalElement("lastError"));
/* 214 */       assertNull("Error timestamp was not reset", ePop3.getAttribute("failingSince", null));
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testIsScheduled()
/*     */     throws Exception
/*     */   {
/* 224 */     Provisioning prov = Provisioning.getInstance();
/* 225 */     Account account = TestUtil.getAccount("user1");
/* 226 */     Map<String, Object> attrs = new HashMap();
/* 227 */     attrs.put("zimbraDataSourceEnabled", "FALSE");
/* 228 */     attrs.put("zimbraDataSourceHost", "testhost");
/* 229 */     attrs.put("zimbraDataSourcePort", "0");
/* 230 */     attrs.put("zimbraDataSourceUsername", "testuser");
/* 231 */     attrs.put("zimbraDataSourcePassword", "testpass");
/* 232 */     attrs.put("zimbraDataSourceFolderId", "1");
/* 233 */     attrs.put("zimbraDataSourceConnectionType", DataSource.ConnectionType.cleartext.toString());
/* 234 */     String name = NAME_PREFIX + " testNegativePollingInterval";
/* 235 */     DataSource ds = prov.createDataSource(account, DataSourceType.pop3, name, attrs);
/*     */     
/*     */ 
/* 238 */     ds = account.getDataSourceByName(name);
/* 239 */     assertFalse(ds.isScheduled());
/*     */     
/*     */ 
/* 242 */     attrs.clear();
/* 243 */     attrs.put("zimbraDataSourcePollingInterval", "0");
/* 244 */     prov.modifyDataSource(account, ds.getId(), attrs);
/* 245 */     ds = account.getDataSourceByName(name);
/* 246 */     assertFalse(ds.isScheduled());
/*     */     
/*     */ 
/* 249 */     attrs.clear();
/* 250 */     attrs.put("zimbraDataSourcePollingInterval", "365d");
/* 251 */     prov.modifyDataSource(account, ds.getId(), attrs);
/* 252 */     ds = account.getDataSourceByName(name);
/* 253 */     assertTrue(ds.isScheduled());
/*     */   }
/*     */   
/*     */   public void testMigratePollingInterval() throws Exception
/*     */   {
/* 258 */     Account account = TestUtil.getAccount("user1");
/* 259 */     Cos cos = account.getCOS();
/*     */     
/*     */ 
/* 262 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 263 */     ZFolder folder = TestUtil.createFolder(mbox, NAME_PREFIX + " testMigratePollingInterval");
/*     */     
/* 265 */     Provisioning prov = Provisioning.getInstance();
/* 266 */     Map<String, Object> attrs = new HashMap();
/* 267 */     attrs.put("zimbraDataSourceEnabled", "FALSE");
/* 268 */     attrs.put("zimbraDataSourceHost", "localhost");
/* 269 */     int port = Integer.parseInt(TestUtil.getServerAttr("zimbraPop3BindPort"));
/* 270 */     attrs.put("zimbraDataSourcePort", Integer.toString(port));
/* 271 */     attrs.put("zimbraDataSourceUsername", "user2");
/* 272 */     attrs.put("zimbraDataSourcePassword", "test123");
/* 273 */     attrs.put("zimbraDataSourceFolderId", folder.getId());
/* 274 */     attrs.put("zimbraDataSourceConnectionType", DataSource.ConnectionType.cleartext.toString());
/* 275 */     String dsName = NAME_PREFIX + " testMigratePollingInterval";
/* 276 */     DataSource ds = prov.createDataSource(account, DataSourceType.pop3, dsName, attrs);
/*     */     
/*     */ 
/* 279 */     account.unsetDataSourcePop3PollingInterval();
/* 280 */     account.unsetDataSourceImapPollingInterval();
/* 281 */     cos.unsetDataSourcePop3PollingInterval();
/* 282 */     cos.unsetDataSourceImapPollingInterval();
/* 283 */     account.setDataSourcePollingInterval("1h");
/* 284 */     cos.setDataSourcePollingInterval("2h");
/*     */     
/*     */ 
/* 287 */     ds.getPollingInterval();
/*     */     
/*     */ 
/* 290 */     account = TestUtil.getAccount("user1");
/* 291 */     cos = account.getCOS();
/* 292 */     ds = account.getDataSourceByName(dsName);
/*     */     
/* 294 */     assertEquals("1h", account.getAttr("zimbraDataSourcePop3PollingInterval"));
/* 295 */     assertEquals("1h", account.getAttr("zimbraDataSourceImapPollingInterval"));
/* 296 */     assertEquals("2h", cos.getAttr("zimbraDataSourcePop3PollingInterval"));
/* 297 */     assertEquals("2h", cos.getAttr("zimbraDataSourceImapPollingInterval"));
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testRss()
/*     */     throws Exception
/*     */   {
/* 307 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 308 */     String parentId = Integer.toString(1);
/* 309 */     ZFolder sourceFolder = TestUtil.createFolder(mbox, "/" + NAME_PREFIX + " testRss source");
/* 310 */     mbox.modifyFolderGrant(sourceFolder.getId(), ZGrant.GranteeType.pub, null, "r", null);
/* 311 */     String subject = NAME_PREFIX + " testRss";
/* 312 */     TestUtil.addMessage(mbox, subject, sourceFolder.getId());
/*     */     
/*     */ 
/* 315 */     String urlString = String.format("https://%s:%s/home/%s%s.rss", new Object[] { TestUtil.getServerAttr("zimbraServiceHostname"), TestUtil.getServerAttr("zimbraMailSSLPort"), "user1", sourceFolder.getPath() });
/*     */     
/* 317 */     urlString = HttpUtil.encodePath(urlString);
/* 318 */     ZFolder rssFolder = mbox.createFolder(parentId, NAME_PREFIX + " testRss destination", null, null, null, urlString);
/*     */     
/*     */ 
/* 321 */     ZRssDataSource ds = (ZRssDataSource)getDataSource(mbox, rssFolder.getId());
/* 322 */     assertNotNull(ds);
/* 323 */     assertNull(mbox.testDataSource(ds));
/*     */     
/*     */ 
/* 326 */     List<ZDataSource> list = new ArrayList();
/* 327 */     list.add(ds);
/* 328 */     mbox.importData(list);
/* 329 */     waitForData(mbox, rssFolder);
/* 330 */     ZMessage syncedMsg = TestUtil.getMessage(mbox, "in:\"" + rssFolder.getPath() + "\"");
/* 331 */     assertEquals(subject, syncedMsg.getSubject());
/*     */     
/*     */ 
/*     */ 
/* 335 */     mbox.deleteFolder(rssFolder.getId());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public void disabledTestCal()
/*     */     throws Exception
/*     */   {
/* 359 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 360 */     String parentId = Integer.toString(1);
/* 361 */     String urlString = "http://www.google.com/calendar/ical/k2kh7ncij3s05dog63g0o0n254%40group.calendar.google.com/public/basic.ics";
/*     */     ZFolder folder;
/*     */     try {
/* 364 */       folder = mbox.createFolder(parentId, NAME_PREFIX + " testCal", ZFolder.View.appointment, null, null, urlString);
/*     */     } catch (ServiceException e) {
/* 366 */       assertEquals("service.RESOURCE_UNREACHABLE", e.getCode());
/* 367 */       ZimbraLog.test.warn("Unable to test calendar data source for %s: %s", new Object[] { urlString, e.toString() });
/* 368 */       return;
/*     */     }
/*     */     
/*     */ 
/* 372 */     ZCalDataSource ds = (ZCalDataSource)getDataSource(mbox, folder.getId());
/* 373 */     assertNotNull(ds);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 378 */     String error = mbox.testDataSource(ds);
/* 379 */     if (error != null) {
/* 380 */       ZimbraLog.test.warn("Unable to test iCal data source for %s: %s.", new Object[] { urlString, error });
/* 381 */       return;
/*     */     }
/*     */     
/*     */ 
/* 385 */     List<ZDataSource> list = new ArrayList();
/* 386 */     list.add(ds);
/* 387 */     mbox.importData(list);
/* 388 */     waitForData(mbox, folder);
/*     */     
/*     */ 
/* 391 */     mbox.deleteFolder(folder.getId());
/* 392 */     mbox.importData(list);
/* 393 */     ds = (ZCalDataSource)getDataSource(mbox, folder.getId());
/* 394 */     assertNull(ds);
/*     */   }
/*     */   
/*     */   private void waitForData(ZMailbox mbox, ZFolder folder) throws Exception
/*     */   {
/* 399 */     for (int i = 1; i <= 10; i++) {
/* 400 */       mbox.noOp();
/* 401 */       if (folder.getSize() > 0L) {
/* 402 */         return;
/*     */       }
/* 404 */       Thread.sleep(500L);
/*     */     }
/* 406 */     fail("No items found in folder " + folder.getPath());
/*     */   }
/*     */   
/*     */   private ZDataSource getDataSource(ZMailbox mbox, String folderId) throws ServiceException
/*     */   {
/* 411 */     for (ZDataSource i : mbox.getAllDataSources()) {
/* 412 */       if (((i instanceof ZRssDataSource)) && (((ZRssDataSource)i).getFolderId().equals(folderId))) {
/* 413 */         return i;
/*     */       }
/*     */     }
/* 416 */     return null;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void tearDown()
/*     */     throws Exception
/*     */   {
/* 424 */     Account account = TestUtil.getAccount("user1");
/* 425 */     Cos cos = account.getCOS();
/*     */     
/* 427 */     account.setDataSourcePollingInterval(this.mOriginalAccountPollingInterval);
/* 428 */     account.setDataSourcePop3PollingInterval(this.mOriginalAccountPop3PollingInterval);
/* 429 */     account.setDataSourceImapPollingInterval(this.mOriginalAccountImapPollingInterval);
/*     */     
/* 431 */     cos.setDataSourcePollingInterval(this.mOriginalCosPollingInterval);
/* 432 */     cos.setDataSourcePop3PollingInterval(this.mOriginalCosPop3PollingInterval);
/* 433 */     cos.setDataSourceImapPollingInterval(this.mOriginalCosImapPollingInterval);
/*     */     
/* 435 */     cleanUp();
/*     */   }
/*     */   
/*     */   public void cleanUp() throws Exception
/*     */   {
/* 440 */     TestUtil.deleteAccount("testdatasource");
/* 441 */     TestUtil.deleteTestData("user1", NAME_PREFIX);
/*     */   }
/*     */   
/*     */   public static void main(String[] args) throws Exception
/*     */   {
/* 446 */     TestUtil.cliSetup();
/* 447 */     TestUtil.runTest(TestDataSource.class);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestDataSource.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */